package com.itfly.controller;

import com.itfly.entity.EventCategories;
import com.itfly.entity.EventManagement;
import com.itfly.exception.BusinessException;
import com.itfly.result.Result;
import com.itfly.service.EventCategoriesService;
import com.itfly.service.EventManagementService;
import com.itfly.utils.JwtUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController//@ResponseBody注解的作用是将Controller方法返回的Java对象通过消息转换器转换为JSON格式数据，写入到响应体中。
@RequestMapping("/category")
@CrossOrigin
public class EventCategoriesController {
    @Autowired
    private EventCategoriesService eventCategoriesService;
    @Autowired
    private EventManagementService eventManagementService;
    //根据id查询事件分类
    @GetMapping("/list")
    public Result<List<EventCategories>> category(@RequestHeader(name = "Authorization") String token) {
        //根据用户名查询用户
        System.out.println(token);
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("id");
        List<EventCategories> category = eventCategoriesService.findCategoryById(userId);
        return new Result<>(1,"查询成功",category);
    }
    //新增分类
    @PostMapping("/add")
    public Result<String> addCategory(@RequestHeader(name = "Authorization") String token,
                                      @RequestBody EventCategories eventCategories) {
        //检查分类名称是否唯一
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("id");
        EventCategories eventCategories1 = eventCategoriesService.selectByName(eventCategories.getCategoryName(), userId);
        if (eventCategories1 != null) {
            throw new BusinessException("事件类型名称不唯一");
        };
        //根据用户名查询用户
        eventCategories.setUserId(userId);
        eventCategoriesService.insertCategory(eventCategories);
        return new Result<>(1,"查询成功",null);
    }
    //查询分类
    @GetMapping("/search")
    public Result<EventCategories> searchCategory(@RequestParam("id") Integer id) {
        EventCategories eventCategories = eventCategoriesService.selectById(id);
        return new Result<>(1,"查询成功",eventCategories);
    }
    //更新分类
    @PutMapping("/update")
    public Result<String> updateCategory(@RequestBody EventCategories eventCategories,
                                         @RequestHeader(name = "Authorization") String token) {
        //检查修改的分类名称是否唯一
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("id");
        EventCategories eventCategories1 = eventCategoriesService.selectByName(eventCategories.getCategoryName(), userId);
        System.out.println(eventCategories1);
        if (eventCategories1 != null) {
            throw new BusinessException("事件类型名称不唯一");
        };
        eventCategoriesService.updateCategory(eventCategories.getCategoryName(),eventCategories.getId());
        return new Result<>(1,"查询成功",null);
    }
    //删除分类
    @RequestMapping("/delete")
    public Result<String> deleteCategory(@RequestParam("id")Integer id) {
        //事件分类如果有对应的事件，则不能删除
        List<EventManagement> eventManagements = eventManagementService.selectByCategoryId(id);
//        if (CollectionUtils.isNotEmpty(eventManagements)) {
//
//            throw new BusinessException("该事件类型有事件记录不能删除");
//        }
        if (eventManagements != null && !eventManagements.isEmpty()) {
            // 如果有关联的事件记录，则不能删除，并返回提示信息
            throw new BusinessException("该事件类型有事件记录不能删除");
        }
        eventCategoriesService.deleteCategory(id);
        return new Result<>(1,"删除成功",null);
    }
}
